package com.smzd.ccms.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.ccms.business.ChannelService;
import com.smzd.ccms.business.SiteChannelConfigService;
import com.smzd.ccms.business.SiteService;
import com.smzd.ccms.domain.Channel;
import com.smzd.ccms.domain.Site;
import com.smzd.ccms.domain.SiteChannelConfig;
import com.smzd.ccms.domain.SysDict;
import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractMeetingOwnedController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.system.business.MeetingService;
import com.smzd.framework.system.business.UserService;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.utils.JsonHelper;
import com.smzd.framework.utils.SmzdPasswordEncoder;
import com.smzd.framework.utils.db.ORPage;
import com.smzd.utils.SessionHolder;

@Controller
@RequestMapping("/admin/sites")
@Access("TAG_ROLE_CCMS")
public class SiteController extends AbstractMeetingOwnedController<Site> {
    @Autowired
    private SiteService service;
    @Autowired
    private SiteChannelConfigService sccService;
    @Autowired
    private ChannelService chService;
    @Autowired
    @Qualifier("md5PasswordEncoder")
    SmzdPasswordEncoder encoder;
    @Autowired
    UserService userService;
    @Autowired
    MeetingService meetingService;

    @Override
    protected IFGService<Site> getService() {
        return service;
    }

    @RequestMapping(value="/add", method=RequestMethod.GET)
    @Override
    public String toAddPage(Map<String, Object> model, Site elem,
            BindingResult result) throws Exception {
        return super.toAddPage(model, elem, result);
    }

    @RequestMapping(value="/add", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String postAddData(Map<String, Object> model, Site elem,
            BindingResult result) throws Exception {
    	String holderName = encoder.encodePassword(elem.getPath(), null);
    	service.createANewSite(elem, getOwner(), holderName);
    	return JsonHelper.getSuccessJsonString("id", elem.getId());
    }

    @RequestMapping(value={"/index.html", ""}, method=RequestMethod.GET)
    @Override
    public String toListPage(Map<String, Object> model, Site elem,
            BindingResult result) throws Exception {
        return super.toListPage(model, elem, result);
    }

    @RequestMapping(value="/list", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String getListData(Map<String, Object> model,ORPage<Meeting, Site> page,
            BindingResult result) throws Exception {
        return super.getListData(model, page, result);
    }

    @RequestMapping(value="/edit", method=RequestMethod.GET)
    @Override
    public String toEditPage(Map<String, Object> model, Site elem,
            BindingResult result) throws Exception {
        elem = service.getItemById(elem.getId());
        return super.toEditPage(model, elem, result);
    }

    @RequestMapping(value="/edit", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String postEditData(Map<String, Object> model, Site elem,
            BindingResult result) throws Exception {
        return super.postEditData(model, elem, result);
    }

    @RequestMapping(value="/remove", method=RequestMethod.POST)
    @ResponseBody
    @Override
    public String postRemoveData(Map<String, Object> model, Site elem,
            BindingResult result) throws Exception {
        return super.postRemoveData(model, elem, result);
    }
    
//    @RequestMapping("/{siteid}/index.html")
//    public String toSiteIndexPage(Map<String, Object> model, @PathVariable("siteid") int siteId){
//    	Site site = service.getItemById(siteId);
//    	SessionHolder.setObject(Site.SESSION_KEY_CURRENT_SITE, site);
//    	model.put("site", site);
//    	User user = MeetingSessionHolder.getLoginUser();
//    	model.put("user", user);
//		SiteChannelConfig scc = sccService.getSiteChannelConfig(site.getOwner().getId(), SysDict.ID_SITE_CH_CONFIG_INDEX);
//		model.put("scc", scc);
//    	return getMyView("site-index.ftl");
////    	return getMyView("newindex.ftl");
//    }
    
    /**
     * TODO 这个方法还在用吗？
     * @param siteId
     * @param chId
     * @return
     * @throws Exception
     */
	@RequestMapping(value = "/{siteid}/indexchannel", method = RequestMethod.POST)
	@ResponseBody
	public String saveIndexChannelConfig(@PathVariable("siteid") int siteId, @RequestParam("chId") int chId)
			throws Exception {
		if (siteId < 0 || chId < 0) {
			return JsonHelper.getFailJsonString("参数不合法！code=0");
		}
		Site site = SessionHolder.getObject(Site.SESSION_KEY_CURRENT_SITE);
		if (siteId - site.getId() != 0) {
			return JsonHelper.getFailJsonString("参数不合法！code=1");
		}
		Channel ch = chService.getItemById(chId);
		if (ch.getOwner().getId() - site.getOwner().getId() != 0) {
			return JsonHelper.getFailJsonString("参数不合法！code=2");
		}
		SiteChannelConfig scc = sccService.getSiteChannelConfig(site.getOwner().getId(),
				SysDict.ID_SITE_CH_CONFIG_INDEX);
		if (scc == null) {
			scc = new SiteChannelConfig();
			scc.setOwner(site.getOwner());
			scc.setSysDict(new SysDict(SysDict.ID_SITE_CH_CONFIG_INDEX));
			scc.setChannel(new Channel(chId));
			scc.setName("site" + siteId + "-index");
			sccService.insert(scc);
		} else {
			scc.setChannel(new Channel(chId));
			sccService.update(scc);
		}
		return JsonHelper.getSuccessJsonString();
	}

}